package cn.cjlmonster.demo02.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.cjlmonster.demo02.Configs;
import cn.cjlmonster.demo02.IEvent;
import cn.cjlmonster.demo02.utils.LanguageUtils;

/**
 * This project is Demo02
 * Created by cjl on 2021/11/7 5:53 下午.
 */

public abstract class BaseAppCompatActivity extends AppCompatActivity {

    protected boolean mIsPause = false;


    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LanguageUtils.getAttachBaseContext(newBase));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(getLayoutId());
        initToolbar();
        initView();
        initData();
        initEvents();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    protected abstract int getLayoutId();

    protected abstract boolean canBack();

    protected void initView() {
    }

    protected void initData() {
    }

    protected void initEvents() {
    }

    private void initToolbar() {
        String title = getIntent().getStringExtra("title");
        if (!TextUtils.isEmpty(title)) setTitle(title);
        if (!canBack()) return;
        ActionBar actionBar = getSupportActionBar();
        if (actionBar == null) return;
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mIsPause = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mIsPause = false;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private Toast mToast;

    protected void showToast(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else mToast.setText(msg);
        mToast.show();
    }

    protected <T extends Activity> void startUI(Class<T> cls, String title) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("title", title);
        startActivity(intent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessage(IEvent event) {
        if (event.getEventCode() == Configs.EVENT_RESTART) {
            restart((Class<?>) event.getData());
            return;
        }
    }

    private void restart(Class<?> cls) {
        if (!mIsPause) startActivity(new Intent(this, cls));
        finish();
    }

    protected void sendReStartEvent(Class<?> cls) {
        IEvent event = new IEvent(Configs.EVENT_RESTART, cls);
        EventBus.getDefault().post(event);
    }
}
